package com.yami.shop.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.MyRecordDto;
import com.yami.shop.bean.model.WxCursor;
import com.yami.shop.bean.model.WxRecord;
import com.yami.shop.bean.param.WxRecordParam;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.WxCursorMapper;
import com.yami.shop.dao.WxRecordMapper;
import com.yami.shop.service.IWxRecordService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgListResp;
import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgSendRequest;
import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfEventMsg;
import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfLinkMsg;
import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表单提交记录表 服务实现类
 * </p>
 *
 * @author kubilewang
 * @since 2022-02-27
 */
@Service
@Slf4j
public class WxRecordServiceImpl extends ServiceImpl<WxRecordMapper, WxRecord> implements IWxRecordService {

    @Autowired
    WxRecordMapper wxRecordMapper;

    @Autowired
    WxCpService wxCpService;


    @Autowired
    WxCursorMapper wxCursorMapper;

    @Override
    public IPage<WxRecord> getList(Page<WxRecord> page, WxRecordParam wxRecordParam) {

        page.setRecords(wxRecordMapper.getList(new PageAdapter(page), wxRecordParam));
        page.setTotal(wxRecordMapper.getCount(wxRecordParam));
        return page;
    }

    @Override
    public void sendMsg(WxCpKfMsgListResp wxCpKfMsgListResp) throws Exception{

        // 保存游标
        WxCursor wxCursor = wxCursorMapper.selectOne(new LambdaQueryWrapper<>());
        if(wxCursor==null){
            wxCursor=new WxCursor();
            wxCursor.setMsgCursor(wxCpKfMsgListResp.getNextCursor());
            wxCursor.setCreateTime(new Date());
            wxCursor.setMsgCursor(wxCpKfMsgListResp.getNextCursor());
            wxCursorMapper.insert(wxCursor);
        }else{
            wxCursor.setUpdateTime(new Date());
            wxCursor.setMsgCursor(wxCpKfMsgListResp.getNextCursor());
            wxCursorMapper.updateById(wxCursor);
        }


        List<WxCpKfMsgListResp.WxCpKfMsgItem> msgList = wxCpKfMsgListResp.getMsgList();
        for (WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem:msgList){

            // 目前只处理用户进入会话事件
            if(wxCpKfMsgItem.getMsgType().equals("event")){

                WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();

                // 发送欢迎语
                if(StringUtils.isNotBlank(event.getWelcomeCode())){
                    WxCpKfMsgSendRequest wxCpKfMsgSendRequest=new WxCpKfMsgSendRequest();
                    wxCpKfMsgSendRequest.setCode(event.getWelcomeCode());
                    wxCpKfMsgSendRequest.setMsgId(wxCpKfMsgItem.getMsgId());
                    wxCpKfMsgSendRequest.setMsgType(wxCpKfMsgItem.getMsgType());
                    wxCpService.getKfService().sendMsgOnEvent(wxCpKfMsgSendRequest);
                }


                WxRecord wxRecord = wxRecordMapper.selectById(event.getScene());
                WxCpKfMsgSendRequest wxCpKfMsgSendRequest=new WxCpKfMsgSendRequest();
                wxCpKfMsgSendRequest.setToUser(event.getExternalUserId());
                wxCpKfMsgSendRequest.setOpenKfid(event.getOpenKfid());
                wxCpKfMsgSendRequest.setMsgId(UUID.randomUUID().toString().replace("-",""));
                wxCpKfMsgSendRequest.setMsgType("link");


                WxCpKfLinkMsg wxCpKfLinkMsg=new WxCpKfLinkMsg();
                wxCpKfLinkMsg.setTitle(wxRecord.getTitle());
                wxCpKfLinkMsg.setDesc(wxRecord.getMiaoshu());
                wxCpKfLinkMsg.setUrl(wxRecord.getCardLink());
                wxCpKfLinkMsg.setThumb_media_id(wxRecord.getPictureUrl());
                wxCpKfMsgSendRequest.setLink(wxCpKfLinkMsg);
                wxCpService.getKfService().sendMsg(wxCpKfMsgSendRequest);

            }else if(wxCpKfMsgItem.getMsgType().equals("miniprogram")){

                String record = wxCpKfMsgItem.getMiniProgram().getPagePath().split("=")[1];
                WxRecord wxRecord = wxRecordMapper.selectById(record);

                WxCpKfMsgSendRequest wxCpKfMsgSendRequest=new WxCpKfMsgSendRequest();
                wxCpKfMsgSendRequest.setToUser(wxCpKfMsgItem.getExternalUserId());
                wxCpKfMsgSendRequest.setOpenKfid(wxCpKfMsgItem.getOpenKfid());
                wxCpKfMsgSendRequest.setMsgId(UUID.randomUUID().toString().replace("-",""));
                wxCpKfMsgSendRequest.setMsgType("link");


                WxCpKfLinkMsg wxCpKfLinkMsg=new WxCpKfLinkMsg();
                wxCpKfLinkMsg.setTitle(wxRecord.getTitle());
                wxCpKfLinkMsg.setDesc(wxRecord.getMiaoshu());
                wxCpKfLinkMsg.setUrl(wxRecord.getCardLink());
                wxCpKfLinkMsg.setThumb_media_id(wxRecord.getPictureUrl());
                wxCpKfMsgSendRequest.setLink(wxCpKfLinkMsg);
                wxCpService.getKfService().sendMsg(wxCpKfMsgSendRequest);

            }else{

                WxCpKfMsgSendRequest wxCpKfMsgSendRequest=new WxCpKfMsgSendRequest();
                wxCpKfMsgSendRequest.setToUser(wxCpKfMsgItem.getExternalUserId());
                wxCpKfMsgSendRequest.setOpenKfid(wxCpKfMsgItem.getOpenKfid());
                wxCpKfMsgSendRequest.setMsgId(UUID.randomUUID().toString().replace("-",""));
                wxCpKfMsgSendRequest.setMsgType("text");
                WxCpKfTextMsg wxCpKfTextMsg=new WxCpKfTextMsg();
                wxCpKfTextMsg.setContent("你真调皮你发我的{"+wxCpKfMsgItem.getText().getContent()+"}已收到。");
                wxCpKfMsgSendRequest.setText(wxCpKfTextMsg);
                wxCpService.getKfService().sendMsg(wxCpKfMsgSendRequest);
            }
        }
    }

    @Override
    public IPage<MyRecordDto> pageMyByUserId(Page<MyRecordDto> page, String userId) {
        page.setRecords(wxRecordMapper.listMyRecordByUserId(new PageAdapter(page), userId));
        page.setTotal(wxRecordMapper.countMyRecordByUserId(userId));
        return page;

    }
}
